home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 815 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A problem with fprintf
  5. Date: Tue, 09 Jan 96 14:34:26 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <821198066snz@genesis.demon.co.uk>
  9. References: <2154942365.28352469@ortel.org>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <2154942365.28352469@ortel.org>
  16.            Ray_Robert@ortel.org "Ray Robert" writes:
  17.  
  18. >vishwajit@cis.ufl.edu writes:
  19. >
  20. >> I am experiencing a peculiar problem with fprintf.
  21. >
  22. >> I have a piece of code where the following statement fits in:
  23. >
  24. >fprintf(fp, "%s\n", src);
  25. >> 
  26. >>fp is a file-pointer and src is a static array of characters. When the
  27. >> code is executed repeatedly, it fails at the above statement. The error
  28. >> message from the debugger is as follows:
  29. >
  30. >> signal SEGV (segmentation violation) in malloc at 0xef778634
  31. >> malloc+0x150:   st      %o0, [%l5]
  32.  
  33. You have probably corrupted your heap before the fprintf call.
  34.  
  35. >> fp is pointing to a file that has been opened and is not the problem.
  36. >> Does somebody have a clue?
  37. >
  38. >Apparently your system's fprintf is doing an malloc() to set up a space in
  39. >which to construct the formatted string (wow! what an inefficiency there). 
  40.  
  41. Unlikely. More likely it allocates the stream buffer the first time the
  42. stream is read/written. It didn't do it when the stream was opened because
  43. at that point the program has the opportunity to set the buffering type
  44. and buffer size with setbuf/setvbuf.
  45.  
  46. >It is probably incorrectly determining the size of "src", which it expects to
  47. >be a (pointer to a) null-terminated string.  One of 2 things is happening (I
  48. >leave it to the academics out there to tell us which is ANSI C):  
  49. >
  50. >(1) The array is being passed by value, i.e., the literal value of the first
  51. >element is being passed to fprintf and hence to malloc, but it's being
  52. >interpretted as if it's a pointer.
  53.  
  54. The 'value' of an array rvalue is a pointer to its first element. This isn't
  55. something that a compiler is likely to get wrong.
  56.  
  57. >(2) The last element of the array is not a null ('\0' in the old style, 0 in
  58. >the new).
  59.  
  60. What styles are you referring to? '\0' and 0 both represent an int constant
  61. with value 0. Stylistically is it sensible to use '\0' to represent a
  62. value that is conceptually a character.
  63.  
  64. -- 
  65. -----------------------------------------
  66. Lawrence Kirby | fred@genesis.demon.co.uk
  67. Wilts, England | 70734.126@compuserve.com
  68. -----------------------------------------
  69.